﻿define([
'Wjs/ui/widgets/fields/FieldBase'
],
function (FieldBase) {
    'use strict';

    var type = 'fileUpload',
    typeManager = Wjs.typeManager,
    FileUpload = typeManager.getType(type);

    if (FileUpload) { // It has been already loaded
        return FileUpload;
    }

    FileUpload = Wjs.typeBuilder.define({
        base: FieldBase,
        construct: function (conf, parent) {
            var me = this,
                form;

            conf = conf || {};

            FileUpload.base.constructor.call(me, conf, parent);

            me.tag = 'input';
            me.setAttributes({
                type: 'file',
                multiple: conf.multiple
            });

            // Set the parent form to enctype="multipart/form-data"
            me.form.setAttributes({ enctype: 'multipart/form-data' });
        },

        type: type,

        getFiles: function () {
            var me = this;

            if (!me.isRendered()) {
                throw 'FileUpload element must be rendered in order to get the files';
            }

            return me.domEl.files;
        }
    });

    typeManager.registerType(FileUpload);

    return FileUpload;
});